﻿using System.Collections;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace WpfMvvmTemplate.WPFUI.Common
{
    public class ComMethod
    {
        /// <summary>
        /// 中英文切换
        /// </summary>
        /// <param name="languageNum">语言编号：0-中文；1-英文</param>
        /// <param name="window">所在的窗体</param>
        /// <param name="languageDic">中英文字典</param>
        public static void LanguageWinDicHelper(int languageNum, object obj)
        {
            string LanguageFile0 = "./Resources/Languages/Chinese.xaml";
            string LanguageFile1 = "./Resources/Languages/English.xaml";

            ResourceDictionary rd = new()
            {
                Source = languageNum == 0
                    ? new Uri(LanguageFile0, UriKind.RelativeOrAbsolute)
                    : new Uri(LanguageFile1, UriKind.RelativeOrAbsolute)
            };

            // 加载窗体window
            if(obj is Window)
                (obj as Window)!.Resources?.MergedDictionaries?.Add(rd);

            if(obj is Page)
                (obj as Page)!.Resources?.MergedDictionaries?.Add(rd);

            // 加载后台字典dic
            Dictionary<string, string> languageDic = new();

            foreach(DictionaryEntry item in rd)
            {
                languageDic.Add(item.Key.ToString() ?? "", item.Value?.ToString() ?? "");
            }

            GlobalStaticData.LanguageDic = languageDic;
        }




    }
}
